let {
  getResourceListDetail,
  InsertData
} = require("../../../api/slibrary.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    zyID: null,
    userInfo: wx.getStorageSync('userInfoMy'),
    showcontent: false,
    data_slibray: {
      zyjb: '一级资源',
      bm: '',
      zylx: '重点企业',
      qy: '苏州',
      zymc: '',
      lxr: "",
      lxrzw: '',
      zynl: '',
      hzsq: '',
      hzlx: '',
      hzkd: '',
      djr: '',
      djxm: '',
      hzjz: '',
      bz: '',
      tname: '',
      type: 2
    },
    showcontent: false,
    select_type: 'zyjb',
    zyjb_select: [{
      text: '一级资源'
    }, {
      text: '二级资源'
    }, {
      text: '三级资源'
    }],
    zylx_select: [],
    types: [{
        text: '全部',
        id: 1,
        fontWeight: '600',
        color: '#333'
      },
      {
        text: '大院大所',
        id: 2,
        fontWeight: '600',
        color: '#666'
      },
      {
        text: '产业基金',
        id: 3,
        fontWeight: '600',
        color: '#666'
      },
      {
        text: '服务机构',
        id: 3,
        fontWeight: '600',
        color: '#666'
      },
      {
        text: '协会/校友会',
        id: 3,
        fontWeight: '600',
        color: '#666'
      },
      {
        text: '重点企业',
        id: 3,
        fontWeight: '600',
        color: '#666'
      },
      {
        text: '政府资源',
        id: 3,
        fontWeight: '600',
        color: '#666'
      },
      {
        text: '其他资源',
        id: 3,
        fontWeight: '600',
        color: '#666'
      },
    ],
  },

  getNewDetail(zyID) {
    getResourceListDetail(zyID).then(res => {
      console.log(this.data.userInfo)
      this.setData({
        detail: res.data[0],
      })
    })

  },

  clickcontent(e) {
    let key = `data_slibray.${this.data.select_type}`;
    this.setData({
      [key]: e.currentTarget.dataset.content,
    })
    this.onClickcontent()
  },
  onClickcontent() {
    this.setData({
      showcontent: !this.data.showcontent
    })

  },
  inputValue(e) {
    let param = e.currentTarget.dataset.name; //字段名
    let value = e.detail; //值
    let key = `data_slibray.${param}`;
    if (param == 'zyjb' || param == 'zylx') {
      if (param == 'zylx') {
        this.setData({
          zylx_select: this.data.types.slice(1),
          select_type: param
        })
      } else {
        this.setData({
          select_type: param
        })
      }
      console.log(this.data.zylx_select)
      setTimeout(() => {
        this.setData({
          showcontent: true,
        })
      }, 500)
    } else {
      this.setData({
        [key]: value,
      });
    }

  },
  onSubmit() {
    console.log(this.data.data_slibray)
    if (this.data.data_slibray.zylx != '' && this.data.data_slibray.zyjb != '' && this.data.data_slibray.zymc != '' && this.data.data_slibray.qy != '' && this.data.data_slibray.zynl != '' && this.data.data_slibray.djr != '') {
      InsertData(this.data.data_slibray).then(res => {
        if (res.status == 500) {
          return wx.showToast({
            title: '对接人非本公司',
            icon: 'none'
          })
        } else {
          wx.showToast({
              title: '修改完成',
              icon: 'none'
            }),
            setTimeout(() => {
              this.onClose()
              this.onLoad({
                zyID: this.data.zyID
              })
            }, 300)
        }
      })
    } else {
      wx.showToast({
        title: '请补充必填项',
        icon: 'none'
      })
    }
  },
  onClose() {
    this.setData({
      show: !this.data.show,
    })

    if (this.data.show == false) {
      this.setData({
        data_slibray: {
          zyjb: '一级资源',
          bm: this.data.userInfo.bm,
          zylx: '重点企业',
          qy: '',
          zymc: '',
          lxr: "",
          lxrzw: '',
          zynl: '',
          hzsq: '',
          hzlx: '',
          hzkd: '',
          djr: this.data.userInfo.jname,
          djxm: '',
          hzjz: '',
          bz: '',
          tname: this.data.userInfo.jname,
          type: 2

        },
      })
    } else {
      this.setData({
        data_slibray: this.data.detail
      })
      this.setData({
        'data_slibray.type': 2
      })
      console.log(this.data.detail, this.data.data_slibray)
    }
  },
  onLoad(options) {
    this.setData({
      zyID: options.zyID
    })
    this.getNewDetail(options.zyID)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})